const NotifyService = require('../service/NotifyService.js')
const GetUserInfoTool = require('../utils/GetUserInfoTool.js')
const SecretTool = require('../utils/SecretTool.js')
const RandomTool = require('../utils/RandomTool.js')
const huaWeiYun = require('../config/huaweiCloud.js')
const BackCode = require('../utils/BackCode.js')
const CodeEnum = require('../utils/CodeEnum.js')
const NotifyController = {
  captch: async(req,res)=>{
    let type = req.query.type
    // 获取用户ip  设备
    const getkey = (req)=>{
      return SecretTool.md5(GetUserInfoTool.getIp(req)+GetUserInfoTool.getUseragent(req))
    }
    let _key = getkey(req)
    let handleRes = await NotifyService.captch(_key, type)
    res.set('content-type','image/svg+xml' )
    res.send(handleRes)
  },
  sendCode: async(req,res)=>{
    let {phone, captcha,type} = req.body
    console.log(req.body, '===')
    // 获取用户ip  设备
    const getkey = (req)=>{
      return SecretTool.md5(GetUserInfoTool.getIp(req)+GetUserInfoTool.getUseragent(req))
    }
    let _key = getkey(req)
    let handleRes = await NotifyService.sendCode(phone, captcha,type,_key,RandomTool.randomCode())
    res.send(handleRes)
  },
  getUrl: async(req,res)=>{
    console.log(req, '---')
    let url = await huaWeiYun.getVideoUrl(req.body.id)
    console.log(url, 'url', url.length)
    let handleRes = null
      if (url.length < 0) {
        handleRes =  BackCode.buildResult(CodeEnum.COURSE_VIDEO_NO_EXIST)
      }
      handleRes =  BackCode.buildSuccessAndData({ data: { playResult: url[0], id:
        req.body.id } })
    res.send(handleRes)
  }
}

module.exports = NotifyController